﻿using CrawlerFramework.CrawlerEngine;
using CrawlerFramework.Models;
using CrawlerFramework.Repository;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CrawlerFramework.Scheduler
{
    public class SchedulerManager : ISchedulerManager
    {
        public SchedulerManager(ICrawlerEngine crawlerEngine)
        {
            this.CrawlerEngine = crawlerEngine;
        }

        public CrawlerEngine.ICrawlerEngine CrawlerEngine
        {
            get;
            private set;
        }

        public IEnumerable<Models.ScheduledJob> GetAllJobs()
        {
            var jobRepo = new BaseCrawlerRepository<ScheduledJob>();
            try
            {
                return jobRepo.All.ToList();
            }
            finally
            {
                jobRepo.Dispose();
            }
        }

        public void Start()
        {
            var jobs = GetAllJobs();
            foreach (var job in jobs)
            {
                var task = new Task(CrawlerEngine, job);
                task.Execute();
            }
        }

        public void StartJob(Guid scheduledJobGuid)
        {
        }
    }
}